package com.ytoglobal.overseas.address.platform.api.controller;

import com.ytoglobal.overseas.address.platform.model.ResponseEntity;
import com.ytoglobal.overseas.address.platform.model.request.AddressRequest;
import com.ytoglobal.overseas.address.platform.model.response.AddressMessageResponse;
import com.ytoglobal.overseas.address.platform.service.AddressMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class AddressMessageController {

    @Autowired
    private AddressMessageService addressMessageService;

    @RequestMapping(value = "/parse", method = {RequestMethod.POST})
    @CrossOrigin
    public ResponseEntity<AddressMessageResponse> parseAddress(@RequestBody AddressRequest addressRequest) {
        AddressMessageResponse addressMessageResponse = addressMessageService.parseAddress(addressRequest);
        return ResponseEntity.ok(addressMessageResponse);
    }

    @RequestMapping(value = "/init", method = {RequestMethod.GET})
    @CrossOrigin
    public ResponseEntity<String> initAddressMetaData() {
        String message = "初始化地址字典库成功";
        try {
            addressMessageService.initAddressMetaData();
        } catch (Exception e) {
            return ResponseEntity.fail(501, "初始化地址字典库失败");
        }
        return ResponseEntity.ok(message);
    }
}
